﻿using System.IO;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SilverlightUpload
{
    public partial class PicItem : UserControl
    {


        #region 定义程序控制变量
        private bool batchUpdate = false;
        #endregion

        #region 属性定义
        private PicItemBase item;
        
        public PicItemBase Item
        {
            get { return item; }
            set
            {
                if (value != null)
                {
                    batchUpdate = true;
                    item = value;
                    if (item.File != null)
                    {
                        using (Stream stream = item.File.OpenRead())
                        {
                            BitmapImage image = new BitmapImage();
                            image.SetSource(stream);
                            //int hsize = image.PixelWidth > image.PixelHeight ? image.PixelWidth : image.PixelHeight;

                            img.Stretch = Stretch.UniformToFill;
                            img.Width = 100;
                            img.Height = 80;
                            img.Source = image;
                            stream.Close();

                            datePicker.SelectedDate = item.TakeDate;
                            textBoxPerson.Text = item.Person;
                            textBoxDescription.Text = item.Description;
                            batchUpdate = false;
                            txtFileSize.Text = 
                                new ByteConverter().Convert(item.FileSize, this.GetType(), null, null).ToString();
                        }
                    }
                }
            }
        }
        #endregion

        #region 定义移除事件
        public class ItemRemoveEventArgument
        {
            
        }

        public delegate void ItemRemoveHandle(object sender, ItemRemoveEventArgument e);

        public event ItemRemoveHandle OnItemRemove;
        #endregion
        public PicItem()
        {
            InitializeComponent();
        }

        #region 编辑事件响应
        private void datePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!batchUpdate)
            if (datePicker.SelectedDate != null) Item.TakeDate = datePicker.SelectedDate.Value;
        }

        private void textBoxPerson_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!batchUpdate) Item.Person = textBoxPerson.Text;
        }

        private void textBoxDescription_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!batchUpdate) Item.Description = textBoxDescription.Text;
        }
        #endregion 编辑事件响应


        private void Image_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            OnItemRemove.Invoke(this, new ItemRemoveEventArgument());
        }
    }
}
